<script>
import { useUserStore } from '../../stores/user'
import DeviceApi from '../../api/device'

export default {
  props: {
    deviceId: {
      type: [String, Number],
      required: true
    },
    deviceTitle: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      bookingForm: {
        startTime: '',
        endTime: '',
        purpose: '',
        remark: ''
      },
      showForm: false,
      loading: false
    }
  },
  computed: {
    userStore() {
      return useUserStore()
    }
  },
  methods: {
    show() {
      this.showForm = true
    },
    close() {
      this.showForm = false
      this.resetForm()
    },
    resetForm() {
      this.bookingForm = {
        startTime: '',
        endTime: '',
        purpose: '',
        remark: ''
      }
    },
    async submitBooking() {
      try {
        this.loading = true
        const api = new DeviceApi()
        const res = await api.createBooking({
          deviceId: this.deviceId,
          startTime: this.bookingForm.startTime,
          endTime: this.bookingForm.endTime,
          purpose: this.bookingForm.purpose
        })
        
        if (res.code === 201) {
          this.$myMessageBox('预约申请已提交，请等待管理员审核')
          this.$emit('booking-success')
          this.close()
        } else {
          this.$myMessageBox(res.msg || '预约失败')
        }
      } catch (err) {
        console.error('Error submitting booking:', err)
        this.$myMessageBox('预约失败，请稍后重试')
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<template>
  <div class="booking-form" v-if="showForm">
    <div class="booking-overlay" @click="close"></div>
    <div class="booking-content">
      <div class="booking-header">
        <h3>预约设备：{{ deviceTitle }}</h3>
        <button class="close-btn" @click="close">&times;</button>
      </div>
      <div class="booking-body">
        <div class="form-group">
          <label>开始时间：</label>
          <input type="datetime-local" v-model="bookingForm.startTime" required>
        </div>
        <div class="form-group">
          <label>结束时间：</label>
          <input type="datetime-local" v-model="bookingForm.endTime" required>
        </div>
        <div class="form-group">
          <label>使用目的：</label>
          <textarea v-model="bookingForm.purpose" placeholder="请简要说明使用目的" required></textarea>
        </div>
        <div class="form-group">
          <label>备注：</label>
          <textarea v-model="bookingForm.remark" placeholder="其他需要说明的事项（选填）"></textarea>
        </div>
      </div>
      <div class="booking-footer">
        <button class="cancel-btn" @click="close">取消</button>
        <button class="submit-btn" @click="submitBooking">提交预约</button>
      </div>
    </div>
  </div>
</template>

<style scoped>
.booking-form {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1000;
  display: flex;
  justify-content: center;
  align-items: center;
}

.booking-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
}

.booking-content {
  position: relative;
  background: white;
  padding: 20px;
  border-radius: 8px;
  width: 90%;
  max-width: 500px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
  z-index: 1001;
}

.booking-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 1px solid #eee;
}

.booking-header h3 {
  margin: 0;
  font-size: 18px;
  color: #333;
}

.close-btn {
  background: none;
  border: none;
  font-size: 24px;
  color: #666;
  cursor: pointer;
  padding: 0;
}

.booking-body {
  max-height: 70vh;
  overflow-y: auto;
  padding-right: 10px;
}

.form-group {
  margin-bottom: 15px;
}

.form-group label {
  display: block;
  margin-bottom: 5px;
  color: #333;
  font-weight: 500;
}

.form-group input,
.form-group textarea {
  width: 100%;
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
  box-sizing: border-box;
}

.form-group textarea {
  height: 80px;
  resize: vertical;
}

.booking-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 20px;
  padding-top: 15px;
  border-top: 1px solid #eee;
}

.cancel-btn,
.submit-btn {
  padding: 8px 20px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s ease;
  min-width: 80px;
  display: inline-block;
}

.cancel-btn {
  background: #f5f5f5;
  border: 1px solid #ddd;
  color: #666;
}

.submit-btn {
  background: #3498db;
  border: 1px solid #3498db;
  color: white;
}

.cancel-btn:hover {
  background: #eee;
}

.submit-btn:hover {
  background: #2980b9;
}
</style> 